<?php

class pdb extends PDO{

	public function __construct($dsn, $user, $passwd) {
		parent::__construct($dsn, $user, $passwd);
		//PDO::ERRMODE_SILENT
		//PDO::ERRMODE_WARNING
		//PDO::ERRMODE_EXCEPTION
		$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	}

	public public function & one($qstr){
		$re	=& $this->query($qstr);
		$re->setFetchMode(PDO::FETCH_ASSOC);
		return $re->fetch();
	}

	public public function & all($qstr){
		$re	=& $this->query($qstr);
		$re->setFetchMode(PDO::FETCH_ASSOC);
		return $re->fetchAll();
	}


	public function insert($table, $fields) {		
		is_array($fields) || $fields = preg_split('/\s*,\s*/', trim($fields) );
		$L = array();
		foreach($fields as $k=>$v) {
			if(empty($v)){
				unset($a[$k]);
			}else{
				$L[$v] = $this->quote($_POST[$v]);
			}
		}
		$sql = 'INSERT INTO ' . $table. ' ('. join(',', $fields) . ') VALUES (' . join(',', $L) . ')';
		$this->query($sql);
		return $this->lastInsertId();
	}

	public function & update($table, $fields, $if) {
		is_array($fields) || $fields = preg_split('/\s*,\s*/', trim($fields) );
		$L = array();
		foreach($fields as $v) {
			if(	!empty($v)	){
				$L[$v] = $v . '=' . $this->quote($_POST[$v]);
			}
		}
		$sql = 'UPDATE ' . $table . ' SET ' . join(',',  $L ) .' WHERE ' . $if;
		return  $this->query($sql);
	}

}

?>